package dyyx;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class HdfsTest {

	private static final String URL = "hdfs://cdh114:8020";
	private static final String HADOOP_HOME = "/Users/dugang/soft/hadoop";
	private static final String TEST_FILE = "/cdh_hdfs_test.txt";

	public static void main(String[] args) throws Exception {

		System.out.println("HdfsTest," + new Date());

		System.setProperty("hadoop.home.dir", HADOOP_HOME);
		Configuration configuration = new Configuration();

		// 如用户无权限，可加用户参数
		FileSystem fileSystem = FileSystem.get(new URI(URL), configuration);

		// java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.

		String str = "hello ,cdh hdfs," + new Date();
		FSDataOutputStream outputStream = fileSystem.create(new Path(TEST_FILE));

		outputStream.write(str.getBytes());
		outputStream.flush();
		outputStream.close();
		
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		FSDataInputStream inputStream = fileSystem.open(new Path(TEST_FILE));
        // IOUtils.copyBytes(inputStream, System.out, 1024);
		
		IOUtils.copyBytes(inputStream, out, 1024);
        inputStream.close();
        
        
        str = new String(out.toByteArray());
   
        System.out.println("\n\n\n\n");

        System.out.println(str);
        
        System.out.println("\n\n\n\n");

        

	}
}
